from app import db
from sqlalchemy.exc import IntegrityError
from app.models import User
from faker import Faker


def generate_users(count=10):
    fake = Faker()
    i = 0
    while i < count:
        u = User(username=fake.user_name(),
                 email=fake.email(),
                 password='123456')
        db.session.add(u)
        try:
            db.session.commit()
            i += 1
            print(f'已创建 {i} 个')
        except IntegrityError:
            db.session.rollback()
    print('用户生成成功')
